﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;

namespace CrmRulo
{
    class ConfigurationFileReader
    {
        private readonly string _configurationFilePath;

        public ConfigurationFileReader(string configurationFilePath)
        {
            _configurationFilePath = configurationFilePath;
        }

        public IEnumerable<Report> ReadReportsInformation()
        {
            var config = XElement.Load(_configurationFilePath);

            string publishDirectory = ".";

            var publishDirNode = config.Attribute("directory");
            if (publishDirNode != null)
                publishDirectory = publishDirNode.Value;

            var reports = new List<Report>();

            foreach (var reportNode in config.Elements("report"))
            {
                var att = reportNode.Attribute("filename");
                Util.Assert(att != null, "Attribute 'filename' not found in report.");
                var reportFileName = String.Format("{0}\\{1}", publishDirectory, att.Value);
                if (!File.Exists(reportFileName))
                {
                    Console.WriteLine(@"ERROR: Report file '{0}' not found. Skipping.", reportFileName);
                    continue;
                }

                var visibilityCodes = reportNode.Elements("visibility").Select(r => (int)Enum.Parse(typeof(ReportVisibilityCode), r.Value)).ToArray();
                var categoryCodes = reportNode.Elements("category").Select(r => (int)Enum.Parse(typeof(ReportCategoryCode), r.Value)).ToArray();
                var entityLogicalNames = reportNode.Elements("entity").Select(r => r.Value).ToArray();

                att = reportNode.Attribute("name");
                Util.Assert(att != null, "Attribute 'name' not found in report.");
                var reportName = att.Value;

                string reportDescription = String.Empty;
                att = reportNode.Attribute("description");
                if (att != null)
                    reportDescription = att.Value;

                var languageCode = -1; // All languages by default
                att = reportNode.Attribute("languagecode");
                if (att != null)
                    languageCode = Int32.Parse(att.Value);

                string parentReportName = null;
                att = reportNode.Attribute("parent");
                if (att != null)
                    parentReportName = att.Value;

                var report = new Report
                {
                    PathToReportFile = reportFileName,
                    Name = reportName,
                    EntityLogicalNames = entityLogicalNames,
                    VisibilityCodes = visibilityCodes,
                    CategoryCodes = categoryCodes,
                    ParentReportName = parentReportName,
                    LanguageCode = languageCode,
                    Description = reportDescription
                };

                reports.Add(report);

            }
            return reports;

        }
    }
}
